/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Process;

import java.net.Socket;



import Main.*;
import java.io.*;
import java.net.*;
import java.util.Vector;

public class ClientProcess implements Runnable {

    Socket s;
    Thread t;
    Vector PlayersList;
    Hearts control;
    InputStream is;
    BufferedReader br;
    OutputStream os;
    BufferedWriter bw;
    String receivedMessage, sentMessage;

    public ClientProcess(Hearts ctrl) {
        control = ctrl;
        PlayersList = new Vector();
        t = new Thread(this);

        try {
            System.out.println(control.PlayerName + " is talking to Server");
            s = new Socket(ctrl.IP, 3200);
            System.out.println(s.getPort());
            is = s.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            os = s.getOutputStream();
            bw = new BufferedWriter(new OutputStreamWriter(os));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //control.ShowClientInterfaceFrame();
        //control.frameClientInterface.setVisible(true);
        t.start();
    }

    public void run() {
        try {

            receivedMessage = br.readLine();
            System.out.println("Received : " + receivedMessage);

            if (receivedMessage.equals("FULL")) {
                bw.write("BYE");
                bw.newLine();
                bw.flush();
            } else {
                bw.write(control.PlayerName);
                bw.newLine();
                bw.flush();
                
            }
            receivedMessage = br.readLine();
            System.out.println("Received : " + receivedMessage);
            
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}
